package cn.owng.server.common.support.id;

import com.littlenb.snowflake.sequence.IdGenerator;
import com.littlenb.snowflake.support.MillisIdGeneratorFactory;
import org.springframework.stereotype.Component;

/**
 * 微信公众号请关注: 开源螺丝钉
 * <br>
 * 码云请关注: <a href="https://gitee.com/xiyoufang">https://gitee.com/xiyoufang</a>
 * <br>
 * 哔哩哔哩请关注: <a href="https://space.bilibili.com/438927834">https://space.bilibili.com/438927834</a>
 *
 * @author xiyoufang
 */
@Component
public class Id {

    private final IdGenerator generator;

    /**
     * 构造一个ID生成器
     *
     * @param idProperties idProperties
     */
    public Id(IdProperties idProperties) {
        this.generator = new MillisIdGeneratorFactory(1609430400000L)
                .create(idProperties.getWorkerId());
    }

    /**
     * 解析 ID
     *
     * @return id
     */
    public String parse(long id) {
        return generator.parse(id);
    }

    /**
     * @return id
     */
    public long next() {
        return generator.nextId();
    }

    /**
     * @return id
     */
    public String id() {
        return String.valueOf(next());
    }
}
